package com.jinlejia.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.RecyclerView;

import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;


/**
 * 更多筛选标签多选Adapter
 */
public class FilterTagMultiAdapter extends YfListAdapter<Map<String, Object>> {

    public int cur_position = -1;
    private Map<Integer, Boolean> checkMap = new HashMap<>();
    private HashSet<String> selectList;
    private Context context;

    public FilterTagMultiAdapter(ArrayList<Map<String, Object>> data, HashSet<String> selectList) {
        super(data);
        this.selectList = selectList;

    }
    public void setData(ArrayList<Map<String, Object>> data) {
        super.setData(data);
        for (int i = 0; i < mData.size(); i++) {
            if ( selectList.contains( mData.get(i).get("valueName").toString() ) ) {
                checkMap.put(i, true);
            } else {
                checkMap.put(i, false);
            }
        }
    }
    public void setSelectList(HashSet<String> selectList){
        this.selectList = selectList;
    }
    @Override
    public int getItemCount() {
        for (int i = 0; i < mData.size(); i++) {
            if ( selectList.contains( mData.get(i).get("valueName").toString() ) ) {
                checkMap.put(i, true);
            } else {
                checkMap.put(i, false);
            }
        }
        return super.getItemCount();
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filter2_3_layout, parent, false);
        context = parent.getContext();
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);

        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(final RecyclerView.ViewHolder viewHolder, final int i) {
        ((ViewHolder) viewHolder).checkbox.setText(mData.get(i).get("valueName").toString());

        if (checkMap.get(i)) {
            ((ViewHolder) viewHolder).checkbox.setChecked(true);
            ((ViewHolder) viewHolder).img.setVisibility(View.VISIBLE);
            ((ViewHolder) viewHolder).layout.setBackgroundResource(R.drawable.dark_gray_bg);
        } else {
            ((ViewHolder) viewHolder).checkbox.setChecked(false);
            ((ViewHolder) viewHolder).img.setVisibility(View.INVISIBLE);
            ((ViewHolder) viewHolder).layout.setBackgroundResource(R.drawable.gray_bg);
        }

        viewHolder.itemView.setOnClickListener(v -> {
            if (checkMap.get(i)) {
                checkMap.put(i,false);
                selectList.remove(mData.get(i).get("valueName").toString());
                if( null != onItemSelectedListener )
                    onItemSelectedListener.onItemRemove(mData.get(i).get("valueName").toString());
            }else{
                checkMap.put(i,true);
                selectList.add(mData.get(i).get("valueName").toString());
                if( null != onItemSelectedListener )
                    onItemSelectedListener.onItemSelected(mData.get(i).get("valueName").toString());
            }
            notifyItemChanged(i);
            System.out.print( selectList.toString() + "  \n" + checkMap.entrySet() );

        });
        viewHolder.itemView.setTag(mData.get(i));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.selling_square_footer, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
    }

    static final class ViewHolder extends RecyclerView.ViewHolder {
        CheckBox checkbox;
        ImageView img;
        LinearLayout layout;

        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.iv_icon);
            checkbox = itemView.findViewById(R.id.checkbox);
            checkbox.setTag(false);
            layout = itemView.findViewById(R.id.layout);
        }
    }


    /**
     * 选中
     */

    public interface OnItemSelectedListener {
        void onItemSelected(String item);
        void onItemRemove(String item);
    }

    private OnItemSelectedListener onItemSelectedListener;

    public void setOnItemSelectedListener(OnItemSelectedListener listener) {
        this.onItemSelectedListener = listener;
    }

}
